<?php

namespace app\api\controller;

class Pushuser
{
    public $pagesize = 10;

    public function bind()
    {
        $ver = trim(input("request.ver"));//app版本
        $imei = trim(input("request.imei"));//imei
        $os = trim(input("request.os"));//系统 1:android ,2：ios ，21：iOS测试版

        $tid = trim(input("request.tid"));//腾讯链接标识
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        if ($os !== '1' && $os != '2' && $os != '3') {
            $return['err'] = 1;
            $return['msg'] = '系统类型不正确';
            return json($return);
        }

        if (empty($tid)) {
            $return['err'] = 2;
            $return['msg'] = '推送标识不正确';
            return json($return);
        }

        $model = new \app\admin\model\TengxunPushuser();
        $model->delData(array('tid' => $tid));

        $data = array(
            'uid' => $userInfo['id'],
            'tid' => $tid,
            'os' => $os,
        );
        $insertID = $model->add($data);
        if (!empty($insertID)) {
            $return['err'] = 0;
            $return['msg'] = '绑定成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '绑定失败';
            return json($return);
        }
    }

    /**
     * 功能页面 val值定义 ： 1、推圈   2、钱包  3、订单   4、团队   5、续期
     * http://api.ituike.net/api/pushuser/push?ty=1&title=nihao&desc=nihaomasdf&val=11
     */
    public function push()
    {

        $title = trim(input("request.title"));//腾讯链接标识
        $desc = trim(input("request.desc"));//腾讯链接标识
        $val = trim(input("request.val"));//腾讯链接标识
        $ty = trim(input("request.ty"));//腾讯链接标识


        $contTemp = array(
            'ty' => $ty,//1、单纯启动APP   2、进商品详情  3、 进活动页面  4、 进功能页面  5、金进入商家详情页面
            'title' => $title,
            'desc' => $desc,
            'val' => $val,
        );


        $ios_cont = array(

            'title' => $desc,
            'content' => $desc,
            "custom" => $contTemp,
            'accept_time' => array(),
            'aps' => array(
                'alert' => $desc,

                'badge' => 5,
                'category' => '',
                'custom1' => 'bar',

                "custom2" => $contTemp,
                "xg" => "oops"
            )

        );


        $cont = json_encode($contTemp, true);
        $msg = array(
            'title' => $title,
            'desc' => $desc,
            'cont' => $cont,
            'cont_ios' => $ios_cont,
            'custom_content' => $contTemp,
            'action' => array(
                'action_type' => 3,
                'activity' => '',
                'intent' => "xgscheme://com.xg.push/notify_detail?param1=" . $cont,

            ),
        );

        $res = pushTengXun($msg);
        return json($res);
    }


    /**
     * 功能页面 val值定义 ： 1、推圈   2、钱包  3、订单   4、团队   5、续期
     * http://api.ituike.net/api/pushuser/pushsingle?ty=1&title=nihao&desc=nihaomasdf&val=11&phone=15982398002
     */
    public function pushsingle()
    {

        $title = trim(input("request.title"));//腾讯链接标识
        $desc = trim(input("request.desc"));//腾讯链接标识
        $val = trim(input("request.val"));//腾讯链接标识
        $ty = trim(input("request.ty"));//腾讯链接标识
        $phone = trim(input("request.phone"));//腾讯链接标识


        $dataModel = new \app\api\model\Users();
        $userInfo = $dataModel->getDataForWhere(array(
            'uname' => $phone
        ));

        if (empty($userInfo)) {
            $res = array(
                'err' => 122,
                'msg' => '用户不存在',
            );
            return json($res);
        }
        $app_source = 0;
        if ($userInfo['addr_type']) {
            $app_source = $userInfo['addr_type'];
        }
        $pushModel = new \app\api\model\Tengxun_pushuser();
        $pushInfo = $pushModel->getDataForWhere(array(
            'uid' => $userInfo['id'],
            'addr_type' => $app_source
        ));
        if (empty($pushInfo)) {
            $res = array(
                'err' => 122,
                'msg' => '用户未登陆',
            );
            return json($res);
        }

        $contTemp = array(
            'ty' => $ty,//1、单纯启动APP   2、进商品详情  3、 进活动页面  4、 进功能页面  5、金进入商家详情页面
            'title' => $title,
            'desc' => $desc,
            'val' => $val,
        );


        $ios_cont = array(

            'title' => $desc,
            'content' => $desc,
            "custom" => $contTemp,
            'accept_time' => array(),
            'aps' => array(
                'alert' => $desc,

                'badge' => 5,
                'category' => '',
                'custom1' => 'bar',

                "custom2" => $contTemp,
                "xg" => "oops"
            )

        );


        $cont = json_encode($contTemp, true);

        $msg = array(
            'title' => $title,
            'desc' => $desc,
            'cont' => $cont,
            'cont_ios' => $ios_cont,
            'custom_content' => $contTemp,
            'action' => array(
                'action_type' => 3,
                'activity' => '',
                'intent' => "xgscheme://com.xg.push/notify_detail?param1=" . $cont,

            ),
        );

        $res = pushTengXunSingle($msg, $pushInfo['tid'], $pushInfo['os'], $app_source);
        return json($res);
    }


    /**
     * 功能页面 val值定义 ： 1、推圈   2、钱包  3、订单   4、团队   5、续期
     * http://api.ituike.net/api/pushuser/pushsingle?ty=1&title=nihao&desc=nihaomasdf&val=11&phone=15982398002
     */
    public function pushmsg()
    {


        $num = 2;//每次获取多少条数据来推送
        $dataModel = new \app\api\model\Push_msg();
        $msgInfoD = $dataModel->getDataForWheres(array(
            'status' => 0
        ), '*', $num);

        if (empty($msgInfoD)) {
            $res = array(
                'err' => 0,
                'msg' => '无推送用消息',
            );
            return json($res);
        }
        if (count($msgInfoD) < $num) {
            $num = count($msgInfoD);
        }
//         print_r($msgInfoD);
        for ($i = 0; $i < $num; $i++) {
            $msgInfo = $msgInfoD[$i];

            $UserDataModel = new \app\api\model\Users();
            $userInfo = $UserDataModel->getDataForWhere(array(
                'id' => $msgInfo['uid']
            ));

            $app_source = 0;
            if ($userInfo['addr_type']) {
                $app_source = $userInfo['addr_type'];
            }

            $pushModel = new \app\api\model\Tengxun_pushuser();
            $pushInfo = $pushModel->getDataForWhere(array(
                'uid' => $msgInfo['uid'],
                'addr_type' => $app_source
            ));


//             print_r($pushInfo);
            if (empty($pushInfo)) {
                $dataModel = new \app\api\model\Push_msg();
                $dataModel->edit(array('id' => $msgInfo['id']), array('status' => 2, 'ptime' => date("Y-m-d H:i:s")));
                $res = array(
                    'err' => 0,
                    'msg' => '用户未登陆',
                );
                continue;
            }

            $ty = $msgInfo['fun'];
            $title = $msgInfo['title'];
            $desc = $msgInfo['cont'];
            $val = $msgInfo['val'];

            $contTemp = array(
                'ty' => $ty,//1、单纯启动APP   2、进商品详情  3、 进活动页面  4、 进功能页面  5、金进入商家详情页面
                'title' => $title,
                'desc' => $desc,
                'val' => $val,
            );


            $ios_cont = array(

                'title' => $desc,
                'content' => $desc,
                "custom" => $contTemp,
                'accept_time' => array(),
                'aps' => array(
                    'alert' => $desc,

                    'badge' => 5,
                    'category' => '',
                    'custom1' => 'bar',

                    "custom2" => $contTemp,
                    "xg" => "oops"
                )

            );


            $cont = json_encode($contTemp, true);

            $msg = array(
                'title' => $title,
                'desc' => $desc,
                'cont' => $cont,
                'cont_ios' => $ios_cont,
                'custom_content' => $contTemp,
                'action' => array(
                    'action_type' => 3,
                    'activity' => '',
                    'intent' => "xgscheme://com.xg.push/notify_detail?param1=" . $cont,

                ),
            );

            $pushInfo['tid'] = trim($pushInfo['tid']);
            $res = pushTengXunSingle($msg, $pushInfo['tid'], $pushInfo['os'], $app_source);
            if ($res['err'] == 0) {
                $dataModel = new \app\api\model\Push_msg();
                $dataModel->edit(array('id' => $msgInfo['id']), array('status' => 1, 'ptime' => date("Y-m-d H:i:s")));
            } else {
                $dataModel = new \app\api\model\Push_msg();
                $dataModel->edit(array('id' => $msgInfo['id']), array('status' => 1, 'ptime' => date("Y-m-d H:i:s")));
            }
        }
        return json($res);
    }

    public function msgLists()
    {
        $auth = trim(input("request.auth"));//学校id
        $pagesize = trim(input("pagesize"));//班级id

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        if (empty($auth)) {
            $return['err'] = 0;
            $return['msg'] = '暂无消息';
            $return['data'] = array();
            return json($return);
        }
        $authUserInfo = parseUserAuth($auth);
        if ($authUserInfo['err'] == 0) {
            $authUserInfo = $authUserInfo['data'];
        }
        if (empty($authUserInfo)) {
            $return['err'] = 0;
            $return['msg'] = '暂无消息';
            $return['data'] = array();
            return json($return);
        }
        $model = new \app\api\model\Push_msg();
        $where = array('uid' => $authUserInfo['id']);

        $lists = $model->lists($where, $pagesize, 'id,uid,title,cont,fun,val,ctime');
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    public function xgl()
    {
        $res = changeFromTKL1();
        print_r($res);
        exit;
//         https://uland.taobao.com/coupon/edetail?e=lFRciHhFn1pt3vqbdXnGltvAYt5xSyf0r2M0iyE9QmkfwGiOA3I23dtcR2RwBkn%2B0clMz3oLbvrhUL2lGzlHMhpywujSvOp2nUIklpPPqYKrAuWeH9vhLftgw96zxPmuDJAE5Mj1u1AqkrzZnyZcSefP9Pb%2FQt6H&af=1&pid=mm_27721785_44354480_448866591

        $data = ('{"e":"lFRciHhFn1pt3vqbdXnGltvAYt5xSyf0r2M0iyE9QmkfwGiOA3I23dtcR2RwBkn%2B0clMz3oLbvrhUL2lGzlHMhpywujSvOp2nUIklpPPqYKrAuWeH9vhLftgw96zxPmuDJAE5Mj1u1AqkrzZnyZcSefP9Pb%2FQt6H","pid":"mm_27721785_44354480_448866591"}');
//         $data=('{"e":"lFRciHhFn1pt3vqbdXnGltvAYt5xSyf0r2M0iyE9QmkfwGiOA3I23dtcR2RwBkn%2B0clMz3oLbvrhUL2lGzlHMhpywujSvOp2nUIklpPPqYKrAuWeH9vhLftgw96zxPmuDJAE5Mj1u1AqkrzZnyZcSefP9Pb%2FQt6H"}');

        $_m_h5_tk = '5e4701fa68a56ee7b8c81f30e4067a63';
        $t = $this->getMillisecond();
        $t = '1536924170418';

        $app_key = '23566532';
        $appSecret = 'ed99380ee05c773471c39c034a890d11';
        //如需固定AP
        $sign = md5($_m_h5_tk . '&' . $t . '&' . $app_key . '&' . $data);
        echo $url = 'https://acs.m.taobao.com/h5/mtop.alimama.union.hsf.coupon.get/1.0/?jsv=2.4.0&appKey=' . $app_key . '&t=' . $t . '&sign=' . $sign . '&api=mtop.alimama.union.hsf.coupon.get&v=1.0&AntiCreep=true&AntiFlood=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=' . $data;

    }


    public function getMillisecond()
    {
        list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
        return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
    }

}
